local skynet = require "skynet"
local filelog = require "filelog"
local msghelper = require "tablehelper"
local timer = require "timer"
local timetool = require "timetool"
local tabletool = require"tabletool"
require "pdkenum"
local tablelogic 	-- = require "tablelogic"
local seatlogic 	-- = require "seatlogic"
local gamelogic 	-- = require "gamelogic"
local ddzlogic

local server, table_data
local TableRequest = {}

function TableRequest.init(base)
    server = base
    table_data = server.table_data
	tablelogic = server.logic.tablelogic
	seatlogic = server.logic.seatlogic
	gamelogic = server.logic.gamelogic
	ddzlogic = server.logic.ddzlogic
end

function TableRequest.update_logic(logic)
	ddzlogic = logic
end

function TableRequest.disconnect(request)
	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		return false
	end
	tablelogic.disconnect(table_data, request, seat)
    return true
end

function TableRequest.entertable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器维护中！"
		return responsemsg
	end

	if table_data.deleting then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "桌子已经删除！"
		return responsemsg
	end

	if msghelper:callmsg_to_gatesvr("check_role_state", request.rid, 0) then
		responsemsg.retcode = EErrCode.ERR_HAD_IN_TABLE
		responsemsg.retstr = "已经在游戏中了！"
		return responsemsg
	end

	--判断玩家金币是否够最小携带
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
		if not request.money.diamond or request.money.diamond < table_data.conf.min_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的金豆不足，请前往充值！"
			return responsemsg
		end
	end
	
	if tablelogic.is_full(table_data) then
		responsemsg.retcode = EErrCode.ERR_TABLE_FULL
		responsemsg.retstr = "房间人数已满！"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)

	tablelogic.entertable(table_data, request, seat)
	
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
		if request.rid < 100 then
			TableRequest.sitdowntable(request)
		end
	end
    return responsemsg
end

function TableRequest.reentertable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if table_data.deleting then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "桌子已经删除！"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
    local waitinfo = table_data.waits[request.rid]
	if seat ~= nil then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
		seat.playerinfo.fangka = request.money.fangka
		seat.getfangka = request.money.fangka
		if table_data.conf.room_type == 1 then
			if seat.getdiamond ~= request.money.diamond then
				local diff = seat.getdiamond - request.money.diamond
				seat.diamond = seat.diamond - diff
				seat.getdiamond = request.money.diamond
				seat.playerinfo.diamond = request.money.diamond
				seat.playerinfo.coin = request.money.coin
			end
		end
		if seat.is_tuoguan == EBOOL.TRUE then
			seat.is_tuoguan = EBOOL.FALSE
			seat.timeout_fold = 0
			seat.is_disconnect = nil
		end
	elseif waitinfo ~= nil then
		waitinfo.playerinfo.rolename = request.playerinfo.rolename
		waitinfo.playerinfo.logo = request.playerinfo.logo
		waitinfo.playerinfo.sex = request.playerinfo.sex		
	end

	if waitinfo == nil and seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效的请求！"
		return responsemsg
	end

	responsemsg.gameinfo = {}
	msghelper:copy_table_gameinfo(responsemsg.gameinfo)
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		local isallready = true
		for k, v in ipairs(table_data.seats) do
			if v.state ~= ESeatState.SEAT_STATE_WAIT_START then
				isallready = false
				break
			end
		end
		if responsemsg.gameinfo.state == ETableState.TABLE_STATE_WAIT_ALL_READY then
			if table_data.cur_games_times > 1 and table_data.cur_games_times <= table_data.conf.game_times and not isallready then
				responsemsg.gameinfo.state = ETableState.TABLE_STATE_WAIT_NEXT_GAMES
			end
		end
	end
	responsemsg.handcards = nil
	responsemsg.dealcards = {}
	responsemsg.cardsput = {}
	responsemsg.action_type = table_data.action_type
	responsemsg.action_to_time = table_data.action_to_time
	responsemsg.action_seat_index = table_data.action_seat_index
	responsemsg.first_action_index = table_data.first_action_index

	if not tablelogic.is_onegameend(table_data) and seat ~= nil then
		responsemsg.handcards = tabletool.deepcopy(seat.cards)
        if table_data.initCards and type(table_data.initCards) == "table" then
		    for k, v in ipairs(table_data.initCards) do
			    table.insert(responsemsg.dealcards,v)
            end
        end
		for k,value in ipairs(table_data.seats) do
			local noticemsg = {
				rid = value.rid,
				putcards = {}
			}
			if value.state == ESeatState.SEAT_STATE_CHUPAI or value.state == ESeatState.SEAT_STATE_FOLLOW_CHUPAI then
				local roundtable = table_data.CardsHeaps[#table_data.CardsHeaps]
				for i = #roundtable,1,-1 do
					if 	roundtable[i].rid == value.rid then
						for m,n in ipairs(roundtable[i].cardHelper) do
							table.insert(noticemsg.putcards,n)
						end
						break
					end
				end
			end
			table.insert(responsemsg.cardsput,noticemsg)
		end
	end
	
	responsemsg.sametabledata = {}
	if table_data.conf.match_type then
		for k, v in ipairs(table_data.seats) do
			if v.sametabledata then
				table.insert(responsemsg.sametabledata, {
					rid = v.rid,
					countdata = v.sametabledata,
				})
			end
		end
	end

	if seat then
		seat.waitreenter = nil
		msghelper:sendmsg_to_tableplayer(seat, "reentertableR", responsemsg)
		tablelogic.reentertable(table_data, request, seat)	 
	end
	-- return responsemsg
end

function TableRequest.leavetable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON
		or table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then --朋友桌房卡场没有退出
		if seat == nil then
			tablelogic.leavetable(table_data, request, seat)
			return responsemsg
		end
		if not tablelogic.is_onegameend(table_data) then
			responsemsg.retcode = EErrCode.ERR_GAMING
			responsemsg.retstr = "游戏中，不能退出!"
			return responsemsg
		end
	end
	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		if request.rid == table_data.conf.create_user_rid and table_data.the_round == 1 then
			tablelogic.leaveallplayers(table_data)
			table_data.deleting = true
			return responsemsg
		end
	end

	tablelogic.standuptable(table_data, request, seat)
	tablelogic.leavetable(table_data, request, seat)	
	return responsemsg
end

function TableRequest.sitdowntable(request)
 	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat ~= nil then
		seat.playerinfo.rolename = request.playerinfo.rolename
		seat.playerinfo.logo = request.playerinfo.logo
		seat.playerinfo.sex = request.playerinfo.sex
		return responsemsg
	else
		--判断玩家金币是否够最小携带
		if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
			if not request.money.diamond or request.money.diamond < table_data.conf.min_carry_diamond then
				responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
				responsemsg.retstr = "您的金豆不足，请前往充值！"
				return responsemsg
			end
		end
		
		if tablelogic.is_full(table_data) then
			if table_data.waits[request.rid] then
				tablelogic.leavetable(table_data, request)
			end

			responsemsg.retcode = EErrCode.ERR_TABLE_FULL
			responsemsg.retstr = "当前桌子已经满了！"
			return responsemsg
		end

		seat = tablelogic.get_emptyseat_by_index(table_data)
		if seat == nil then
			if table_data.waits[request.rid] then
				tablelogic.leavetable(table_data, request)
			end
			responsemsg.retcode = EErrCode.ERR_NO_EMPTY_SEAT
			responsemsg.retstr = "当前桌子没有空座位了！"
			return responsemsg	
		end
	end
	tablelogic.sitdowntable(table_data, request, seat)

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_SYSTEM then
		if request.rid < 100 then
			TableRequest.gameready(request)
		end
	end
    return responsemsg
end

function TableRequest.standuptable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat then
		if seat.state == ESeatState.SEAT_STATE_PLAYING then
			responsemsg.retcode = EErrCode.ERR_GAMING
			responsemsg.retstr = "游戏中，不能退出"
			return responsemsg
		end
	else
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "你已经站起了！"
		return responsemsg
	end
	tablelogic.standuptable(table_data, request, seat)
	return responsemsg
end

function TableRequest.gameready(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if server.will_kill then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "服务器即将维护，请退出房间！"
		return responsemsg
	end

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_ALL_READY then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！不是等待准备状态"
		filelog.sys_error("TableRequest.gameready: 无效请求！不是等待准备状态")
		return responsemsg	
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_FRIEND then
		if table_data.cur_games_times >= table_data.conf.game_times then
			responsemsg.retcode = EErrCode.ERR_GAME_TIMES_OVER
			responsemsg.retstr = "牌桌局数已经打完！"
			filelog.sys_error("TableRequest.gameready: 牌桌局数已经打完！")
			return responsemsg
		end
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		filelog.sys_error("TableRequest.gameready: 玩家不在座位上！")
		return responsemsg
	end
	if seat.state ~= ESeatState.SEAT_STATE_WAIT_READY then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "座位状态不是等待准备状态！"
		filelog.sys_error("TableRequest.gameready: 座位状态不是等待准备状态！")
		return responsemsg
	end

	if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
		if seat.diamond < table_data.conf.leave_carry_diamond then
			responsemsg.retcode = EErrCode.ERR_NOT_ENOUGH_DIAMOND
			responsemsg.retstr = "您的金豆不足，请前往充值！"
			filelog.sys_error("TableRequest.gameready: 砖石不够了，先充值吧！", seat.diamond, table_data.conf.leave_carry_diamond)
			return responsemsg
		end
	end

	responsemsg.state = ESeatState.SEAT_STATE_WAIT_START
	tablelogic.gameready(table_data, request, seat)
	return responsemsg
end

function TableRequest.startgame(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	if table_data.state ~= ETableState.TABLE_STATE_WAIT_ALL_READY then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！"
		return responsemsg
	end
	
	if not tablelogic.startgame(table_data, request) then
		responsemsg.retcode = EErrCode.ERR_PLAYER_NOTENOUGH
		responsemsg.retstr = "最小人数不足"
		return responsemsg
	end
	return responsemsg
end

local function check_cards_exist(handcards, putcards)
	local exist = 0
	for _, c in ipairs(putcards) do
		for _, v in ipairs(handcards) do
			if c == v then
				exist = exist + 1
				break
			end
		end
	end
	return exist == #putcards
end

function TableRequest.doaction(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS, 
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end

	-- 不需要托管了
	if request.action_type == EActionType.ACTION_TYPE_REQUEST_TUOGUAN then
		return
	end

	if request.action_type == EActionType.ACTION_TYPE_CHECK and table_data.action_type ~= EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "无效请求！错误的操作!"
		return responsemsg
	end

	if request.action_type ~= EActionType.ACTION_TYPE_REQUEST_TUOGUAN and
		request.action_type ~= EActionType.ACTION_TYPE_CANCEL_TUOGUAN then
		if (request.action_type ~= EActionType.ACTION_TYPE_CHECK and table_data.action_type ~= request.action_type) or
			table_data.action_seat_index ~= seat.index then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "无效请求！没有轮到你出牌！"
			filelog.sys_error("doaction: not your turn!!")
			return responsemsg
		end
	end
	local putcards = {}
	if request.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI or request.action_type == EActionType.ACTION_TYPE_CHUPAI then
		--TO ADD 牌大小合法性判断
		---如果是该回合第一个出牌,则只判断牌型的合法性
		if seat.is_tuoguan == EBOOL.TRUE then
			return responsemsg
		end

		if request.cards == nil or #request.cards == 0 then
			responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
			responsemsg.retstr = "无效牌型！出的牌不能为空！"
			return responsemsg
		end

		if not check_cards_exist(seat.cards, request.cards) then
			responsemsg.retcode = EErrCode.ERR_CARDS_NOT_EXIST
			responsemsg.retstr = "出的牌不存在！"
			filelog.sys_error("doaction: put cards are not exist!!")
			return responsemsg
		end
		putcards = tabletool.deepcopy(request.cards or {})
		local cardHelper = ddzlogic.CreateCardsHelper(request.cards)
		cardHelper:GetCardsType(cardHelper)		--牌形识别
		--[[
		local cardtype = {	-- test use
			"UNKNOWN",
			"单张",
			"对子",
			"三张",
			"炸弹",
			"火箭(王炸)",
			"单顺",
			"连对",
			"三顺",
			"三带一",
			"三带二",
			"飞机带翅膀(单)",
			"飞机带翅膀(双)",
			-- "四带二张",
			-- "四代两对",
			"四代三张",
			"软炸弹",
			"天炸弹",
		}
		-- filelog.warn("doaction: 牌形识别--->", cardHelper.m_eCardType, cardtype[cardHelper.m_eCardType + 1])
		--]]
		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_UNKNOWN then	--cardHelper.m_eCardType表示玩家将要出的牌的牌形
			responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
			responsemsg.retstr = "无效牌型！"
			return responsemsg
		end

		if cardHelper.m_nLen ~= #request.cards then
			responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
			responsemsg.retstr = "无效牌型！"
			return responsemsg
		end
		if request.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
			----取牌堆最顶上的牌,比较大小
			local roundheaps = table_data.CardsHeaps[#table_data.CardsHeaps]
			if #table_data.CardsHeaps == 0 or roundheaps[#roundheaps].cardHelper == nil then
				responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
				responsemsg.retstr = "无效牌型！"
				return responsemsg
			else
				local roundheap = table_data.CardsHeaps[#table_data.CardsHeaps]
				local compareflag = cardHelper:CompareCards(roundheap[#roundheap].cardHelper,cardHelper)
				if compareflag == false then
					responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
					responsemsg.retstr = "你出的牌太小,大不过上家！"
					return responsemsg
				end
			end
		end

		-------------------------- recheck ----------------------------
		if not table_data.winner or table_data.conf.blackheart3 == 1 then
			if not table_data.CardsHeaps or #table_data.CardsHeaps == 0 then	-- 第一手牌检查是否带了最小牌
				local min = false
				if seat.cards[#seat.cards] == 0 or seat.cards[#seat.cards - 1] == 0
					or seat.cards[#seat.cards - 2] == 0 or seat.cards[#seat.cards - 3] == 0 then	-- 有黑桃3找之
					for _, v in ipairs(request.cards) do
						if v == 0 then
							min = true
							break
						end
						if min then break end
					end
				else	-- 没有黑桃3找最小
					local _, mincard = ddzlogic.checkblackheart3(table_data.seats)
					for _, v in ipairs(request.cards) do
						if v == mincard then
							min = true
							break
						end
						if min then break end
					end
				end
				if not min then
					responsemsg.retcode = EErrCode.ERR_CARDS_NOT_EXIST
					responsemsg.retstr = "必须从黑桃3或者最小牌开始出！"
					filelog.sys_error("必须从黑桃3或者最小牌开始出!")
					return responsemsg
				end
			end
		end
		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_THREE_ONE then	-- 最后一手才能三带一
			if #seat.cards > 4 then
				responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
				responsemsg.retstr = "无效牌型！"
				filelog.sys_error("无效牌型, 最后一手才能三带一!")
				return responsemsg
			end
		end

		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_THREE_STRAIGHT then	-- 最后一手才能三不带
			local tmpcards = tabletool.deepcopy(seat.cards)
			for k,v in ipairs(request.cards) do
				for m,n in ipairs(tmpcards) do
					if v == n then table.remove(tmpcards, m) end
				end
			end
			if #tmpcards > 0 then
				responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
				responsemsg.retstr = "无效牌型！"
				filelog.sys_error("无效牌型, 最后一手才能三不带!")
				return responsemsg
			end
		end

		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_THREE_WING_ONE then	-- 最后一手才能飞机带单
			local tmpcards = tabletool.deepcopy(seat.cards)
			for k,v in ipairs(request.cards) do
				for m,n in ipairs(tmpcards) do
					if v == n then table.remove(tmpcards, m) end
				end
			end
			if #tmpcards > 0 then
				responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
				responsemsg.retstr = "无效牌型！"
				filelog.sys_error("无效牌型, 最后一手才能飞机带单!")
				return responsemsg
			end
		end
		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_THREE_STRAIGHT then	-- 最后一手才能飞机不带
			local tmpcards = tabletool.deepcopy(seat.cards)
			for k,v in ipairs(request.cards) do
				for m,n in ipairs(tmpcards) do
					if v == n then table.remove(tmpcards, m) end
				end
			end
			if #tmpcards > 0 then
				responsemsg.retcode = EErrCode.ERR_INVALID_CARDTYPE
				responsemsg.retstr = "无效牌型！"
				filelog.sys_error("无效牌型, 最后一手才能飞机不带!")
				return responsemsg
			end
		end

		----从玩家手牌上删除
		for k,v in ipairs(request.cards) do
			for m,n in ipairs(seat.cards) do
				if v == n then table.remove(seat.cards,m) end
			end
		end

		----牌堆中
		if table_data.CardsHeaps == nil then
			table_data.CardsHeaps = {}
		end

		-- 炸弹记录，当玩家打出一个炸弹，并且没有被更大的炸弹接起，则视为该玩家炸弹得分，牌局结束后统一结算。
		if cardHelper.m_eCardType == ECardType.DDZ_CARD_TYPE_BOMB then
			local status
			for _, _seat in ipairs(table_data.seats) do
				if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
					if _seat.rid ~= seat.rid then
						status = ddzlogic.getCardsbyCardType(_seat.cards, cardHelper, true, false)
						if status then
							break
						end
					end
				end
			end
			--[[
			local roundheaps = table_data.CardsHeaps[#table_data.CardsHeaps]
			if roundheaps and #table_data.CardsHeaps > 0 and roundheaps[#roundheaps].cardHelper then
				local roundheap = table_data.CardsHeaps[#table_data.CardsHeaps]
				local helper = roundheap[#roundheap].cardHelper
				local playerid = roundheap[#roundheap].rid
				if helper.m_eCardType == ECardType.DDZ_CARD_TYPE_BOMB and seat.rid ~= playerid then
					table_data.bomb[playerid] = table_data.bomb[playerid] - 1
					local bombdel = 0
					local bombindex = nil
					for _, _seat in ipairs(table_data.seats) do
						if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
							 if _seat.rid ~= playerid then
								_seat.diamond = _seat.diamond + 10*table_data.conf.base_score
								if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
									if _seat.bomboff then
										_seat.diamond = _seat.diamond + _seat.bomboff
									end
									bombdel = bombdel + 10*table_data.conf.base_score + (_seat.bomboff or 0)
									_seat.bomboff = nil
								else
									bombdel = bombdel + 10*table_data.conf.base_score
								end
								msghelper:sendmsg_to_alltableplayer("moneychangeN", {
									money_change_list = {
										{
											rid = _seat.rid,
											money_type = 2,
											value = _seat.diamond,
										},
									}
								})
							else
								bombindex = _seat.index
							end
						end
					end
					table_data.seats[bombindex].diamond = table_data.seats[bombindex].diamond - bombdel
					msghelper:sendmsg_to_alltableplayer("moneychangeN", {
						money_change_list = {
							{
								rid = playerid,
								money_type = 2,
								value = table_data.seats[bombindex].diamond,
							},
						}
					})
				end
			end
			--]]
			if not status then
				table_data.bomb[seat.rid] = (table_data.bomb[seat.rid] or 0) + 1
				local bombadd = 0
				for _, _seat in ipairs(table_data.seats) do
					if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
						if _seat.index ~= seat.index then
							_seat.diamond = _seat.diamond - 10*table_data.conf.base_score
							if table_data.conf.room_type == ERoomType.ROOM_TYPE_COMMON then
								if _seat.diamond < 0 then
									_seat.bomboff = _seat.diamond
									_seat.diamond = 0
								end
								bombadd = bombadd + 10*table_data.conf.base_score + (_seat.bomboff or 0)
							else
								bombadd = bombadd + 10*table_data.conf.base_score
							end
							msghelper:sendmsg_to_alltableplayer("moneychangeN", {
								money_change_list = {
									{
										rid = _seat.rid,
										money_type = 2,
										value = _seat.diamond,
									},
								}
							})
						end
					end
				end
				seat.diamond = seat.diamond + bombadd
				msghelper:sendmsg_to_alltableplayer("moneychangeN", {
					money_change_list = {
						{
							rid = seat.rid,
							money_type = 2,
							value = seat.diamond,
						},
					}
				})
				table_data.bomb_index = nil	-- 一定要清空bomb_index
			else
				table_data.bomb_index = seat.index
			end
		end

		local heapOne = {
			rid = request.rid,
			cardHelper = tabletool.deepcopy(cardHelper)
		}
		
		if request.action_type == EActionType.ACTION_TYPE_CHUPAI then
			table_data.CardsHeaps[#table_data.CardsHeaps+1] = {}
			table.insert(table_data.CardsHeaps[#table_data.CardsHeaps],heapOne)
		elseif request.action_type == EActionType.ACTION_TYPE_FOLLOW_CHUPAI then
			table.insert(table_data.CardsHeaps[#table_data.CardsHeaps],heapOne)
		end
	elseif request.action_type == EActionType.ACTION_TYPE_CHECK then
		local status = tablelogic.followcards(table_data, true)
		if status == true then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "能打必打，不能过牌！"
			filelog.sys_error("能打必打，不能过牌！")
			return responsemsg
		end
		table_data.action_type = EActionType.ACTION_TYPE_CHECK
	elseif request.action_type == EActionType.ACTION_TYPE_REQUEST_TUOGUAN then
		---玩家托管请求
		--[[
		if seat.is_tuoguan == EBOOL.TRUE then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "在托管中,不能重复托管"
			-- base.skynet_retpack(responsemsg)
		elseif seat.is_tuoguan == EBOOL.FALSE then
			-- base.skynet_retpack(responsemsg)
			tablelogic.dealtuoguan(table_data,seat)
		end
		return responsemsg
		--]]
		return
	elseif request.action_type == EActionType.ACTION_TYPE_CANCEL_TUOGUAN then
		---玩家取消托管
		if seat.is_tuoguan == EBOOL.FALSE then
			responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
			responsemsg.retstr = "未在托管中,不能取消托管"
		elseif seat.is_tuoguan == EBOOL.TRUE then
			seatlogic.canceltuoguan(table_data,seat)
		end
		return responsemsg
	end
	
	tablelogic.doaction(table_data, request, seat)

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_SYSTEM then
		local action = timetool.get_time() .. "_" 
			.. seat.rid .. "_"
			.. (request.action_type or '0')
		tablelogic.save_actions(table_data, action, putcards)
	end

	return responsemsg
end

function TableRequest.disbandtable(request)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS,
	}

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_FRIEND then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是自建桌,不能解散"
		return responsemsg
	end

	-- if not tablelogic.is_gameend(table_data) then
	-- 	responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
	-- 	responsemsg.retstr = "游戏已经开始不能解散"
	-- 	return responsemsg
	-- end

	if table_data.vote_timerid then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "已经有人提起投票了"
		return responsemsg
	end

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_HAD_STANDUP
		responsemsg.retstr = "玩家不在座位上！"
		return responsemsg
	end

	local vote_wait_time = 40
	local vote_to_time = timetool.get_time() + vote_wait_time
	local votentcmsg = {
		-- rid = request.rid,
		-- name = request.playerinfo.rolename,
		-- wait_to_time = vote_to_time,
		rid = request.rid,
		seat_index = seat.index,
		action_timeout_time = vote_to_time,
		apply_player_name = request.playerinfo.rolename,
		player_disband_list = {},
	}

	for _, seat in ipairs(table_data.seats) do
		if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and seat.rid > 0 then
			if seat.rid == request.rid then
				table.insert(votentcmsg.player_disband_list, {
					rid = seat.rid,
					rolename = seat.playerinfo.rolename,
					is_agree = 1,
				})
			else
				table.insert(votentcmsg.player_disband_list, {
					rid = seat.rid,
					rolename = seat.playerinfo.rolename,
				})
			end
		end
	end
	table_data.vote_rid = request.rid
	table_data.vote_name = request.playerinfo.rolename
	table_data.vote_timeout = vote_to_time
	msghelper:sendmsg_to_alltableplayer("disbandtableN", votentcmsg)
	table_data.vote_timerid = timer.settimer(vote_wait_time*100, "disbandtable")
	TableRequest.disbanddeal({rid = request.rid, playerinfo = request.playerinfo, is_agree = 1}, true)
	return responsemsg
end

function TableRequest.disbanddeal(request, nonotice)
	local responsemsg = {
		-- retcode = EErrCode.ERR_SUCCESS,
	}

	local seat = tablelogic.get_seat_by_rid(table_data, request.rid)
	if seat == nil then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是游戏玩家,不能投票!"
		return responsemsg
	end

	if table_data.conf.room_type ~= ERoomType.ROOM_TYPE_FRIEND then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "不是自建桌,不能投票!"
		return responsemsg
	end

	if not table_data.vote_timerid then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "投票已经结束!"
		return responsemsg
	end

	if table_data.vote_answers and table_data.vote_answers[seat.index] then
		responsemsg.retcode = EErrCode.ERR_INVALID_REQUEST
		responsemsg.retstr = "已经投过票了!"
		return responsemsg
	end

	if not request.is_agree or request.is_agree == 1 then
		table_data.vote_answers = table_data.vote_answers or {}
		table_data.vote_answers[seat.index] = 1
	else
		table_data.vote_num = (table_data.vote_num or 0) + 1	-- 记录不同意的数量，方便结果处理
		-- table_data.vote_names = table_data.vote_names or {}
		-- table.insert(table_data.vote_names, request.playerinfo.rolename)

		table_data.vote_answers = table_data.vote_answers or {}
		table_data.vote_answers[seat.index] = 2
	end

	if not nonotice then
		-- local agree = 0
		-- for _ in pairs(table_data.vote_answers or {}) do
		-- 	agree = agree + 1
		-- end
		local answerntcmsg = {
			rid = request.rid,
			-- agree_num = agree,
			rolename = request.playerinfo.rolename,
			-- is_agree = request.is_agree,
			player_disband_list = {},
		}

		for _, _seat in ipairs(table_data.seats) do
			if _seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER and _seat.rid > 0 then
				table.insert(answerntcmsg.player_disband_list, {
					rid = _seat.rid,
					rolename = _seat.playerinfo.rolename,
					is_agree = table_data.vote_answers[_seat.index],
				})
			end
		end

		msghelper:sendmsg_to_alltableplayer("disbanddealN", answerntcmsg)
		-- msghelper:sendmsg_to_alltableplayer("answerdisbandtableN", answerntcmsg)
	end
	-- local pnum = table_data.conf.max_player_num
	-- if table_data.conf.game_type == EGameType.GAME_TYPE_NORMAL_FOR2 or table_data.conf.game_type == EGameType.GAME_TYPE_CARD15_FOR2 then
	-- 	pnum = table_data.conf.max_player_num - 1
	-- end
	local pnum = table_data.sitdown_player_num
	if (table_data.vote_num and table_data.vote_num > 0) or (table_data.vote_answers and #table_data.vote_answers == pnum) then
		-- local noteresultntcmsg = {
		-- 	result = 0,
		-- 	names = table_data.vote_names,
		-- }

		local noteresultntcmsg = {
			table_index = table_data.conf.idx,
			is_success = false,
		}
		if not table_data.vote_num or table_data.vote_num == 0 then
			noteresultntcmsg.is_success = true
		end
		msghelper:sendmsg_to_alltableplayer("disbandresultN", noteresultntcmsg)
		timer.cleartimer(table_data.vote_timerid)
		table_data.vote_timerid = nil
		table_data.vote_rid = nil
		table_data.vote_name = nil
		table_data.vote_timeout = nil
		table_data.vote_num = nil
		table_data.vote_answers = nil
		-- table_data.vote_names = nil

		if noteresultntcmsg.is_success then
			if table_data.timer_id and table_data.timer_id > 0 then
				timer.cleartimer(table_data.timer_id)
				table_data.timer_id = -1
			end
			for _, seat in ipairs(table_data.seats) do
				if seat.state ~= ESeatState.SEAT_STATE_NO_PLAYER then
					tablelogic.passive_standuptable(table_data, seat, EStandupReason.STANDUP_REASON_DELETE_TABLE)
				end
			end
			-- server.delete(table_data)
			table_data.delete_table_timer_id = timer.settimer(0, "delete_table")
		end
	end
	return responsemsg
end

return TableRequest